<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/27 0027
 * Time: 22:09
 */

namespace App\Listener;

use Firebase\JWT\JWT;
use Lib\Core\ConfigLoad;
use Lib\Core\WebSocket\WebSocketContext;

/**
 * @Listener(ws.close)
 * @description:客户端断开 从哈希表移除绑定关系
 * @auth:yfl
 * @email:554665488@qq.com
 * Class OpenListener
 * @package App\Listener
 */
class CloseListener
{
    public function handel($params)
    {
//        var_dump("CloseListener事件触发");
        $server = $params['server'];
        $fd = $params['fd'];
        $redis = $params['redis'];
        $request = WebSocketContext::getRequest($fd);
        $token = $request->header['sec-websocket-protocol'];
        $config = ConfigLoad::getInstance();
        $config->load();//载入配置文件
        $key = $config::get('JwT_key');
        $parseToken = JWT::decode($token, $key, ['HS256']);
        $userInfo = $parseToken->data;

        if ($redis->hDel($userInfo->service_url, 'uid_' . $userInfo->uid)) {
            echo 'uid' . $userInfo->uid . '下线';
        }

    }
}